package cn.com.oceansoft.business.os.api.vote.controller;

import cn.com.oceansoft.business.base.entity.Result;
import cn.com.oceansoft.business.os.api.qrcode.domain.User;
import cn.com.oceansoft.business.os.api.vote.domain.Show;
import cn.com.oceansoft.business.os.api.vote.service.IVoteService;
import cn.com.oceansoft.nos.cache.redis.CacheBaseService;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("api/vote")

public class VoteController {
    @Resource
    private CacheBaseService cacheBaseService;
    @Resource
    private IVoteService iVoteService;


    @RequestMapping(value = "gotoVote", method = RequestMethod.GET)
    public String gotoScan(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        String status = String.valueOf(cacheBaseService.getObject("status"));
        if ("0".equals(status)) {
            modelMap.addAttribute("msg", "投票尚未开始，请耐心等待！");
            return "errors/osyear_404";
        } else if ("2".equals(status)) {
            modelMap.addAttribute("msg", "投票已结束，更多活动请持续关注！");
            return "errors/osyear_404";
        }
        //TODO 验证投票时间是否正确
        String openId = (String) request.getSession().getAttribute("openId");
        if (null == openId || "".equals(openId)) {
            modelMap.addAttribute("msg", "会话过期，请关闭后重试！");
            return "errors/osyear_404";
        }
        User user = iVoteService.findUserByOpenId(openId);
        //验证当前用户是否已经投票
        if (null == user) {
            modelMap.addAttribute("msg", "请签到后进行投票！");
            return "errors/osyear_404";
        }
        int i = iVoteService.countVote(String.valueOf(user.getId()));
        if (i > 0) {
            modelMap.addAttribute("msg", "您已投票，投票结果请关注大屏幕！");
            return "errors/osyear_404";
        }

        List<Show> shows = new ArrayList<Show>();
        shows = (List<Show>) cacheBaseService.getObject("shows");
        if (null == shows || shows.size() == 0) {
            shows = iVoteService.getshowListAll();
            cacheBaseService.cache("shows", shows);
        }
        modelMap.addAttribute("shows", shows);
        return "osyear/toupiao";
    }

    @RequestMapping(value = "endVote", method = RequestMethod.GET)
    public String endVote(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        modelMap.addAttribute("msg", "投票成功！投票结果请关注大屏幕！");
        return "errors/osyear_404";
    }

    // 投票
    @ResponseBody
    @RequestMapping(value = "uservote", method = RequestMethod.POST)
    public Result uservote(HttpServletRequest request, HttpServletResponse response) {
        //获取用户
        String openId = (String) request.getSession().getAttribute("openId");
        User user = iVoteService.findUserByOpenId(openId);
        String showids = request.getParameter("showids");
        String[] s = showids.split(",");
        for (int i = 0; i < s.length; i++) {
            iVoteService.uservote(s[i], user.getId());
        }
        return new Result(true, "投票成功!");
    }


}
